#include "utils.h"
#include "stdio.h"

#define GB 1073741824
#define MB 1048576
#define KB 1024

#define HOUR 3600
#define MINUTE 60
#define DAY 86400

void FormatRate(char *transfer_rate_str, long long bytes)
{
    printf("bytes:%lld\n", bytes);
    if(bytes < 0){
        sprintf(transfer_rate_str, "Unkown");
    }else if(bytes >= GB){
        sprintf(transfer_rate_str, "%.2fGB/s", bytes * 1.0 / GB);
    }else if(bytes >= MB){
        sprintf(transfer_rate_str, "%.2fMB/s", bytes * 1.0 / MB);
    }else if(bytes >= KB){
        sprintf(transfer_rate_str, "%.2fKB/s", bytes * 1.0 / KB);
    }else {
        sprintf(transfer_rate_str, "%lldB/s", bytes);
    }
}

void FormatByte(char *size_str, long long bytes)
{
    printf("bytes:%lld\n", bytes);
    if(bytes < 0){
        sprintf(size_str, "Unkown");
    }else if(bytes >= GB){
        sprintf(size_str, "%.2fGB", bytes * 1.0 / GB);
    }else if(bytes >= MB){
        sprintf(size_str, "%.2fMB", bytes * 1.0 / MB);
    }else if(bytes >= KB){
        sprintf(size_str, "%.2fKB", bytes * 1.0 / KB);
    }else {
        sprintf(size_str, "%lldB", bytes);
    }
}


void FormatTime(char *estimate_time_str, long long estimate_time)
{
    if(estimate_time >= DAY ||estimate_time == 0){
        sprintf(estimate_time_str, "%s", ">1day");
    }
    else{
        int hours = estimate_time / HOUR;
        int mins = estimate_time % HOUR / MINUTE;
        int secs = estimate_time % MINUTE;
        sprintf(estimate_time_str, "%02d:%02d:%02d", hours, mins, secs);
    }
}